package com.my.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.demo.pojo.*;
import com.my.demo.service.FamilyInformationService;
import com.my.demo.service.IdentifyService;
import com.my.demo.service.PerIdentifyService;
import com.my.demo.service.PersonService;

import com.my.demo.utils.DeleteDir;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import jnr.ffi.annotations.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IdentifyController {
  @Autowired
  PersonService personService;
  @Autowired
  IdentifyService identifyService;
  @Autowired
  FamilyInformationService familyInformationService;
  @Autowired
  PerIdentifyService perIdentifyService;
  @Autowired
  DeleteDir deleteDir;

  /*
   * 网页点击复核详细信息，进入复核详细信息页面，给前端发送该用户所拥有的家谱
   * 后端给前端返回一个家谱信息文件的page
   * 其中page.pages是总页数，page.current是当前页的页码，page.total是总记录数
   */
  @PostMapping("/begin_identify")
  public Result getGenealogyFiles(@RequestBody Map<String,Integer> map,
      HttpSession session){

    Result result = new Result();
    result.setSuccess(false);
    result.setMsg(null);

    Integer pn = map.get("pn");
    if (pn == null) {
      System.out.println("获取到前端的pn："+pn );
      pn=1;
    }


    Integer userId = null;
    try {
      userId = (Integer) session.getAttribute("id");//获取用户id
    }catch (Exception exception){
      exception.printStackTrace();
    }

    QueryWrapper<FamilyInformation> queryWrapper= new QueryWrapper<>();
    queryWrapper.eq("uploader",userId);
    queryWrapper.orderByDesc("upload_time");
    try {
      Page<FamilyInformation> familyInformationPage = new Page<FamilyInformation>(pn,5);//一页设置5条记录
      Page<FamilyInformation> page = familyInformationService.page(familyInformationPage,queryWrapper);
      result.setDetail(page);
    }catch (Exception exception){
      exception.printStackTrace();
    }

    result.setSuccess(true);
    result.setMsg("获取数据成功");

    return result;
  }

  /*
   * 获取复核的信息：前端传入家谱id：family_archive_id以及当前复核的页码：page_number
   * 后端给前端传入对应的图片的信息list2以及复核内容的信息list1
   * 后端更新家谱信息表中对应家谱的状态变为已复核
   */
  @PostMapping("/get_identify")
  @CrossOrigin
  public Result getIdentify(@RequestBody Map<String, Integer> map){
    Result result = new Result();
    result.setSuccess(false);
    result.setMsg(null);

    Integer family_archive_id = map.get("family_archive_id");
    JSONObject jsonObject =new JSONObject();
    QueryWrapper<Identify> identifyQueryWrapper = new QueryWrapper<>();
    identifyQueryWrapper.eq("family_archive_id",family_archive_id);
    identifyQueryWrapper.orderByAsc("page_number");
    List<Identify> list1 = identifyService.list(identifyQueryWrapper);

    QueryWrapper<PerIdentify> perIdentifyQueryWrapper = new QueryWrapper<>();
    perIdentifyQueryWrapper.eq("family_archive_id",family_archive_id);
    perIdentifyQueryWrapper.orderByAsc("page_number");
    List<PerIdentify> list2 = perIdentifyService.list(perIdentifyQueryWrapper);

    jsonObject.put("identify_detail",list1);
    jsonObject.put("picture_detail",list2);

    result.setSuccess(true);
    result.setMsg("选择家谱成功");
    result.setDetail(jsonObject);

    UpdateWrapper<FamilyInformation> familyInformationUpdateWrapper =new UpdateWrapper<>();
    familyInformationUpdateWrapper.eq("family_archive_id",family_archive_id).set("state",3);
    familyInformationService.update(familyInformationUpdateWrapper);

    return result;
  }



  /*
   * 前端点击查看或者复核，后端要给到复核的表
   * 前端需要传给后端的参数：要复核的家谱的编号family_archive_id
   * 获取要复核的文件的信息
   */
  @PostMapping("/delete_identify")
  public Result delete_identify(@RequestBody Map<String,Integer> map){
    Result result =new Result();
    result.setSuccess(false);
    result.setDetail(null);

    Integer id=map.get("id");
    FamilyInformation familyInformation = familyInformationService.getById(id);

    familyInformationService.removeById(id);


    deleteDir.delFolder(System.getProperty("user.dir")+"\\family_archives\\"+familyInformation.getName());

    result.setSuccess(true);
    result.setMsg("删除成功");

    return  result;
  }


}
